#include "expendmenu.h"
#include <QSqlQuery>
#include <QDateTime>
#include <QDebug>
#include <QObject>
#include <QThread>
#include <QSqlError>

ExpendMenu::ExpendMenu()
{
}

void ExpendMenu::updateExpendMenu(QStandardItemModel* model, QProgressDialog *progressDialog){

    QSqlQuery *sqlQuery = new QSqlQuery;
    QModelIndex index;
    QString query_str;
    int id;
    QString name;


    for (int row = 0; row < model->rowCount(); row++)
    {
        progressDialog->setValue(row);
        index = model->index(row,ExpendMenu::ID_COL);
        id = model->data(index).toInt();

        index = model->index(row,ExpendMenu::NAME_COL);
        name = model->data(index).toString();
        if (name.trimmed().length() < 3) return;
        if (id == 0)
        {
            query_str = "INSERT INTO expend_others_menu "
                    "(name)"
                    " VALUES "
                    "('"
                      + name +"'"+
                    ")";
            qDebug() << query_str;
            sqlQuery->prepare(query_str);
            sqlQuery->exec();
        }else{

            query_str  = "UPDATE expend_others_menu SET "
                        "name ='"         + name       + "'"+
                        + " where id ="+ QString::number(id);
            qDebug() << query_str;
            sqlQuery->prepare(query_str);
            sqlQuery->exec();
        }


    }

}
